#include "StdAfx.h"
#include "DrawHoughCircle.h"


CDrawHoughCircle::CDrawHoughCircle(void)
{
}


CDrawHoughCircle::~CDrawHoughCircle(void)
{
}
BEGIN_MESSAGE_MAP(CDrawHoughCircle, CDrawOpenCV)
	ON_WM_PAINT()
END_MESSAGE_MAP()


void CDrawHoughCircle::DealwithFrame()
{
	// TODO: Add your command handler code here
	if(m_pFrame == NULL)
	{			
		return ;
	}
	if(m_pShow == NULL)
	{

	}
	else
	{
		cvReleaseImage( &m_pShow );
	}

    //IplImage* m_pShow = NULL;
    IplImage* pImg8U = NULL;
    CvMemStorage* storage = NULL;
    CvSeq* circles = NULL;

	if (m_pFrame->nChannels==3) {
        m_pShow = cvCloneImage( m_pFrame );
	}
	else {
 		m_pShow = cvCreateImage(cvGetSize(m_pFrame), IPL_DEPTH_8U, 3);
		cvCvtColor(m_pFrame, m_pShow, CV_GRAY2BGR);
	}
	cvFlip(m_pShow);
     
    pImg8U = cvCreateImage( cvGetSize(m_pShow), 8, 1 );
	cvCvtColor(m_pShow, pImg8U, CV_BGR2GRAY);
    cvSmooth( pImg8U, pImg8U, CV_GAUSSIAN, 7, 7 );
 
	storage = cvCreateMemStorage(0);

    circles = cvHoughCircles( pImg8U, storage, CV_HOUGH_GRADIENT,
					2, pImg8U->height/10, 100, 55 );

    int i;
    for( i = 0; i < circles->total; i++ )
    {
        float* p = (float*)cvGetSeqElem(circles,i);
        cvCircle( m_pShow, cvPoint(cvRound(p[0]), cvRound(p[1])), 
					cvRound(p[2]),CV_RGB(255,0,0), 1, CV_AA, 0 );
    }

    cvReleaseMemStorage( &storage );
    cvReleaseImage( &pImg8U );

	//cvFlip(m_pShow);
	//m_dibFlag=imageReplace( m_pShow, &m_pFrame);
 // 	
	//m_ImageType=-2;
	//Invalidate();


}
